home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / Hungry Homer / CW Source / SpriteEngine.c next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  5.5 KB  |  287 lines  |  [TEXT/MMCC]

  1. #include "SpriteTools.h"
  2.  
  3.  
  4. Boolean gDone = false;
  5. short    TheMenuItem;
  6. Boolean killed = false;
  7.  
  8. static void InitSpriteEngine(Rect *offBounds, short backgroundPictureID)
  9. {
  10.     PicHandle    backgroundPicture;
  11.     GDHandle saveGD;
  12.     GWorldPtr savePort;
  13.     
  14.     MyNewGWorld(&gOffScreen, offBounds);
  15.     MyNewGWorld(&gBackScreen, offBounds);
  16.     
  17.     GetGWorld(&savePort, &saveGD);
  18.     SetGWorld((GWorldPtr)gBackScreen, nil);
  19.     
  20.     EraseRect(offBounds);
  21.     
  22.     backgroundPicture = GetPicture(backgroundPictureID);
  23.     if (backgroundPicture != nil)
  24.     {
  25.         DrawPicture(backgroundPicture, offBounds);
  26.         ReleaseResource((Handle)backgroundPicture);
  27.     }
  28.     SetGWorld((GWorldPtr)gOffScreen, nil);
  29.     CopyBits(&gBackScreen->portBits, &gOffScreen->portBits, offBounds, offBounds, srcCopy, nil);
  30.     SetGWorld((GWorldPtr)myWindow, GetMainDevice());
  31.     CopyBits(&gOffScreen->portBits, &myWindow->portBits, offBounds, offBounds, srcCopy, nil);
  32.     SetGWorld(savePort, saveGD);
  33. }
  34.  
  35.  
  36. static void RunSpriteEngine()
  37. {
  38. #define  kWallBounce 7
  39. #define  kBallDiameterSquared (32*32)
  40.  
  41.     Rect tmpRect;
  42.     GDHandle saveGD;
  43.     GWorldPtr savePort;
  44.     SpritePtr    theSprite, anotherSprite;
  45.     Rect    bounds1, bounds2;
  46.  
  47.     GetGWorld(&savePort, &saveGD);
  48.  
  49.     SetGWorld((GWorldPtr)gOffScreen, nil);
  50.  
  51.     theSprite = gSpriteList;
  52.     while (theSprite != nil)
  53.     {
  54.         theSprite->drawingRect = theSprite->face->portRect;
  55.         OffsetRect(&theSprite->drawingRect, theSprite->position.h, theSprite->position.v);
  56.         CopyBits(&gBackScreen->portBits, &gOffScreen->portBits, &theSprite->drawingRect, &theSprite->drawingRect, srcCopy, nil);
  57.         
  58.         theSprite = theSprite->next;
  59.     }
  60.  
  61.     theSprite = gSpriteList;
  62.     while (theSprite != nil)
  63.     {
  64.         MoveSprite(theSprite);
  65.  
  66.         theSprite = theSprite->next;
  67.     }
  68.  
  69.     theSprite = gSpriteList;
  70.     while (theSprite != nil)
  71.     {
  72.         bounds1 = theSprite->face->portRect;
  73.         OffsetRect(&bounds1, theSprite->position.h, theSprite->position.v);
  74.  
  75.         anotherSprite = theSprite->next;
  76.         while (anotherSprite != nil)
  77.         {
  78.             bounds2 = anotherSprite->face->portRect;
  79.             OffsetRect(&bounds2, anotherSprite->position.h, anotherSprite->position.v);
  80.  
  81.             if (SectRect(&bounds1, &bounds2, &tmpRect))
  82.                 HitSprite(theSprite, anotherSprite);
  83.  
  84.             anotherSprite = anotherSprite->next;
  85.         }
  86.         theSprite = theSprite->next;
  87.     }
  88.  
  89.     theSprite = gSpriteList;
  90.     while (theSprite != nil)
  91.     {
  92.         tmpRect = theSprite->face->portRect;
  93.         OffsetRect(&tmpRect, theSprite->position.h, theSprite->position.v);
  94.         PlotFace(theSprite->face, gOffScreen, *(Point *)&tmpRect);
  95.  
  96.         theSprite = theSprite->next;
  97.     }
  98.  
  99.     SetGWorld((GWorldPtr)myWindow, saveGD);
  100.     theSprite = gSpriteList;
  101.     while (theSprite != nil)
  102.     {
  103.         CopyBits(&gOffScreen->portBits, &myWindow->portBits, &theSprite->drawingRect, &theSprite->drawingRect, srcCopy, nil);
  104.         theSprite->drawingRect = theSprite->face->portRect;
  105.         OffsetRect(&theSprite->drawingRect, theSprite->position.h, theSprite->position.v);
  106.         CopyBits(&gOffScreen->portBits, &myWindow->portBits, &theSprite->drawingRect, &theSprite->drawingRect, srcCopy, nil);
  107.  
  108.         theSprite = theSprite->next;
  109.     }
  110.     SetGWorld(savePort, saveGD);
  111. };
  112.  
  113.  
  114.  
  115. static void InitToolbox(void) {
  116.     InitGraf (&qd.thePort);
  117.     InitFonts ();
  118.     FlushEvents (everyEvent,0);
  119.     InitWindows ();
  120.     InitMenus ();
  121.     TEInit ();
  122.     InitDialogs (nil);
  123.     InitCursor ();
  124. }
  125.  
  126.  
  127.  
  128. static void InitStuff()
  129. {
  130.     Rect    windowRectangle;
  131.  
  132.     SetRect(&windowRectangle, 50, 50, 512, 342);
  133.     myWindow = NewCWindow(nil, &windowRectangle, "\pRurl", true, documentProc, (WindowPtr)-1L, false, 0);
  134.     SetPort(myWindow);
  135.  
  136.     qd.randSeed = TickCount ();
  137. }
  138.  
  139.  
  140. #define    kBackgroundPictureID    128
  141.  
  142.  
  143. void main(void)
  144. {    
  145.     InitToolbox();
  146.     Initialize();
  147.     EventLoop();
  148.     ShowCursor();
  149.     
  150.     FlushEvents(mDownMask, 0);
  151. }
  152.  
  153. void startGame(void)
  154. {
  155.     long    startTime;
  156.     initMySprites();
  157.     while (!killed)
  158.     {
  159.         startTime = TickCount();
  160.         RunSpriteEngine();
  161.         while (TickCount() < startTime + kFrameTime);
  162.     }
  163. }
  164.  
  165. void initMySprites(void)
  166. {
  167.     InitStuff();
  168.     InitSpriteEngine(&myWindow->portRect, kBackgroundPictureID);
  169.     InitSprites();
  170. }
  171.  
  172. void    EventLoop()
  173. {
  174.     EventRecord        theEvent;
  175.     short            thePart;
  176.     BitMap            screenBits;
  177.     WindowPtr        myWindow;
  178.     long            WhatMenu;
  179.     short            TheMenu;
  180.     
  181.     while(gDone != true)
  182.     {
  183.         WaitNextEvent( everyEvent, &theEvent, 0L, 0L );
  184.         switch( theEvent.what )
  185.         {
  186.             case mouseDown:
  187.                 thePart = FindWindow( theEvent.where, &myWindow );
  188.                 switch( thePart )
  189.                 {
  190.                     case inDrag:
  191.                         if ( (myWindow != FrontWindow ()) && ((theEvent.modifiers & cmdKey) == 0) )
  192.                             SelectWindow(myWindow);
  193.                         DragWindow(myWindow, theEvent.where, &qd.screenBits.bounds);
  194.                     break;
  195.     
  196.                     case inMenuBar:
  197.                         WhatMenu = MenuSelect( theEvent.where );
  198.                         if( WhatMenu != 0)
  199.                         {
  200.                             TheMenu = HiWord( WhatMenu );
  201.                             TheMenuItem = LoWord( WhatMenu );
  202.                             switch( TheMenu )
  203.                             {
  204.                                 case 128:
  205.                                     DoAppleMenu();
  206.                                 break;
  207.                         
  208.                                 case 129:
  209.                                     DoFileMenu();
  210.                                 break;
  211.                             }
  212.                         }
  213.                     HiliteMenu(0);
  214.                     break;
  215.                 }    
  216.             break;        
  217.  
  218.             case keyDown:
  219.                 case autoKey:
  220.                 CheckKey(&theEvent);
  221.             break;
  222.         }
  223.     }        
  224. }    
  225.  
  226. void    DoAppleMenu()
  227. {    
  228.     switch( TheMenuItem )
  229.     {
  230.         case 1:
  231.             SysBeep(0);    
  232.         break;
  233.     }
  234. }
  235.  
  236. void Quit()
  237. {
  238.     gDone = true;
  239. }
  240.  
  241. void    CheckKey(EventRecord *theEvent)
  242. {
  243.     char        theChar;
  244.     Boolean        commandDown;
  245.     
  246.     theChar = theEvent->message & charCodeMask;
  247.     commandDown = ((theEvent->modifiers & cmdKey) != 0);
  248.     
  249.     if (commandDown)
  250.     {
  251.         switch(theChar)
  252.         {
  253.             case 'q':
  254.                 Quit();
  255.             break;
  256.             
  257.             case 'n':
  258.                 startGame();
  259.             break;
  260.         }
  261.     }
  262. }
  263.  
  264. void    DoFileMenu()
  265. {
  266.     switch( TheMenuItem )
  267.     {
  268.         case 1:
  269.             startGame();
  270.         break;
  271.                                         
  272.         case 3:
  273.             Quit();
  274.         break;
  275.     }
  276. }
  277.  
  278.  
  279. void Initialize()
  280. {
  281.     Handle            MenuBarHandle;
  282.     WindowPtr        myWindow;
  283.     
  284.     MenuBarHandle = GetNewMBar( 128 );
  285.     SetMenuBar( MenuBarHandle );
  286.     DrawMenuBar();
  287. }